package com.biwin.app.controller;

import cn.hutool.json.JSONUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author biwin
 * @since
 */
@RestController("/test")
public class IndexController implements ApplicationContextAware {

    private List<String> requestMappings = new ArrayList<>();

    @GetMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @GetMapping("/test")
    public String api(){
        return "api";
    }

    @GetMapping("/mappings")
    public String getAllRequestMapping(){
        return JSONUtil.toJsonStr(requestMappings);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AbstractHandlerMethodMapping<RequestMappingInfo> methodMapping = (AbstractHandlerMethodMapping<RequestMappingInfo>) applicationContext.getBean("requestMappingHandlerMapping");
        Map<RequestMappingInfo, HandlerMethod> mapRet = methodMapping.getHandlerMethods();
        mapRet.keySet().forEach(requestMappingInfo -> {
            requestMappings.add(requestMappingInfo.toString());
            System.err.println(requestMappingInfo.getPatternsCondition().getPatterns());

        });
    }
}
